<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
    <link rel="stylesheet" href="/static/css/bootstrap.min.css">
    <link rel="stylesheet" href="/static/css/login.css">
</head>

<body>
    <div class="container">
        <div class="row">
            <div class="col-sm">

            </div>
            <div class="col-sm">
                <div class="card" style="width: 30rem;">
                    <div class="card-body shadow">
                        <form>

                            <div class="form-group row">
                                <label for="inputEmail3" class="col-sm-2 col-form-label">用户名</label>
                                <div class="col-sm-10">
                                  <input type="email" class="form-control" id="inputEmail3">
                                </div>
                              </div>

                              <div class="form-group row">
                                <label for="inputPassword3" class="col-sm-2 col-form-label">密码</label>
                                <div class="col-sm-10">
                                  <input type="password" class="form-control" id="inputPassword3">
                                </div>
                              </div>

                              <div class="form-group row">
                                <label for="inputPassword3" class="col-sm-2 col-form-label" >确认密码</label>
                                <div class="col-sm-10">
                                  <input type="password" class="form-control" id="inputPassword3">
                                </div>
                              </div>

                            <div class="form-group btn">
                                <button type="button" class="btn btn-success" onclick="jump()">取消</button>
                                <button type="submit" class="btn btn-success" onclick="jump()">注册</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
            <div class="col-sm">

            </div>
<!-- 
            <script>
                function jump(){
                    window.location.href="./login.html";
                }
            </script> -->
        </div>
    </div>
    <script src="/static/js/jquery-3.5.1.min.js"></script>
    <script src="/static/js/bootstrap.min.js"></script>

    <script>
      function jump() {
            // 获取表单数据
            let uid = $('#username').val();
            let pwd = $('#password').val();
            let confirmpwd = $('#confirmpassword').val();

            // 判断用户名、密码是否为空，为空则提示；不为空则向服务器提交
            if (uid.length > 0 && pwd.length > 0 && pwd === confirmpwd) {
                axios({
                    method: 'post',
                    url: '/registerDone',
                    data: $('#registerForm').serialize(),
                }).then(function (res) {
                    // axios工具下，服务器真正返回的数据在返回对象的data中
                    res = res.data;
                    if (res.code === 200) {
                        window.location.href = '/login';
                    } else {
                        alert_fn(res.msg);
                        console.log(res.msg);
                    }
                }).catch((err) => {
                    alert_fn(err);
                    console.log(err);
                });
            } else {
                alert_fn('用户名、密码不能为空，并且两次密码应该一致');
            }
        }

        // 包装了显示（以及自动隐藏）消息警告的动作
        function alert_fn(msg){
            $('#msgId').text(msg);
            $('.alert').removeClass('invisible');
            setTimeout(() => {
                $('.alert').addClass('invisible');
            }, 3000);
        }
    </script>
</body>

</html>